﻿using System;
using dotNetMagazin201108.BusinessLayer;
using System.Collections.Generic;
using System.Linq;

namespace dotNetMagazin201108.PersistenceLayer
{
    public class CompanyRepositoryImpl : CompanyRepository
    {
        private List<Company> DummyCompanies;
        private int nextCompanyId = 1;
        private static CompanyRepositoryImpl Instance = null;

        private CompanyRepositoryImpl()
        {
            DummyCompanies = new List<Company>();
            DummyCompanies.Add(CreateCompany(nextCompanyId++, "Google Inc.", 100));
            DummyCompanies.Add(CreateCompany(nextCompanyId++, "Apple Inc.", 1));
            DummyCompanies.Add(CreateCompany(nextCompanyId++, "Microsoft Corp.", 80));
            DummyCompanies.Add(CreateCompany(nextCompanyId++, "SAP AG", 10000));
        }

        public static CompanyRepositoryImpl GetInstance()
        {
            if (Instance == null) Instance = new CompanyRepositoryImpl();
            return Instance;
        }

        public IEnumerable<Company> GetAll()
        {
            return DummyCompanies;
        }

        public Company GetById(int id)
        {
            Company company = DummyCompanies.Where(c => c.Id == id).SingleOrDefault();
            return company;
        }

        public bool Add(Company company)
        {
            company.Id = nextCompanyId++;
            DummyCompanies.Add(company);
            return true;
        }

        public bool Update(Company company)
        {
            throw new NotImplementedException();
        }

        private Company CreateCompany(int id, string name, int size) 
        {
            Company c = new Company();

            c.Id = id;
            c.Name = name;
            c.Size = size;

            return c;
        }
    }
}
